package com.drakulo.games.ais;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Properties;

import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.SlickException;

import com.drakulo.games.ais.core.GameData;
import com.drakulo.games.ais.core.Settings;
import com.drakulo.games.ais.core.io.BuildingIO;
import com.drakulo.games.ais.ui.I18n;
import com.drakulo.games.ais.ui.state.MainMenuState;
import com.drakulo.games.ais.ui.state.SectorState;
import com.drakulo.games.ais.ui.state.PlanetState;
import com.drakulo.games.ais.ui.state.ResearchState;
import com.drakulo.games.ais.ui.twlbridge.TWLStateBasedGame;

/**
 * <h1>The Game</h1>
 * <p>
 * Initialize the game's states and start the rendering
 * </p>
 * 
 * @author Drakulo
 * 
 */
public class AloneInSpace extends TWLStateBasedGame {
	private static String version;
	private static String releaseType;
	static {

		Properties props = new Properties();
		InputStream inputStream = AloneInSpace.class.getClassLoader()
				.getResourceAsStream("version.properties");

		if (inputStream == null) {
			System.out.println("Version file not found!");
		}

		try {
			props.load(inputStream);
			version = props.getProperty("version");
			releaseType = props.getProperty("releaseType");
		} catch (IOException e1) {
			e1.printStackTrace();
		}
	}

	// public static final int PLAY_STATE = 0;
	public static final int SECTOR_STATE = 0;
	public static final int RESEARCH_STATE = 1;
	public static final int MAIN_MENU_STATE = 2;
	public static final int PLANET_STATE = 3;

	public static boolean showConsole = false;

	public static void main(String[] args) throws Exception {
		if(args != null && args.length >0){
			Settings.IS_DEBUG = true;
		}
		if(Settings.IS_DEBUG){
			System.out.println("== Debug mode enabled ==");
		}
		
		AppGameContainer app = new AppGameContainer(new AloneInSpace());
		app.setDisplayMode(Settings.WIDTH, Settings.HEIGHT, false);
		app.setFullscreen(Settings.FULL_SCREEN);
		app.setShowFPS(false);
		app.setMinimumLogicUpdateInterval(16);
		app.setTargetFrameRate(60);
		app.start();
	}

	public AloneInSpace() throws Exception {
		super("Alone in Space" + " - v" + version + "-" + releaseType);
		// I18n init
		I18n.initialize();

		GameData.initialize();
		
		// Loading building configs
		// TODO Handle file corruption
		BuildingIO.loadBuildingsData();
	}

	@Override
	public void initStatesList(GameContainer arg0) throws SlickException {
		this.addState(new MainMenuState(MAIN_MENU_STATE));
		this.addState(new SectorState(SECTOR_STATE));
		this.addState(new ResearchState(RESEARCH_STATE));
		this.addState(new PlanetState(PLANET_STATE));
		
		this.enterState(MAIN_MENU_STATE);
	}

	@Override
	protected URL getThemeURL() {
		System.out.println("Getting theme file");
		return AloneInSpace.class
				.getResource("/com/drakulo/games/ais/ui/config/theme.xml");
	}

}
